var ajaxErrorHandler = function(){
	console.log('ajax error');
	alert('Could not connect with server.');
};


var ajaxCompleteHandler = function(){
	$.unblockUI();
};

var login = function(username, password, mode){
	$.blockUI();
	
	$.getJSON('login.service', {
		username : username,
		password : password,
		mode : mode
	})
	.success(function(data){
		console.log(data.status + ', reason:' + data.reason);
		if(data.status=='OK'){
			$.cookie('username', $('#username').val());
			$.cookie('hashed_password', data.hashed_password);
			$.cookie('session_id', data.session_id);
			alert('Login successfully.');
			location = 'system:login=' + $('#username').val();
		}else{
			$.removeCookie('username');
			$.removeCookie('hashed_password');
			$.removeCookie('session_id');
			alert('Login failed.' + data.reason);
		}
	})
	.error(ajaxErrorHandler)
	.complete(ajaxCompleteHandler);
};